package com.qianfeng.missfood.ui;

import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.qianfeng.missfood.R;
import com.qianfeng.missfood.adapter.Category_Detail_ptrListView_adapter;
import com.qianfeng.missfood.adapter.Category_xiangqing_RecyclerView_Adapter;
import com.qianfeng.missfood.category_bean.Category_Bean;
import com.qianfeng.missfood.category_bean.Category_Detail_Bean;
import com.qianfeng.missfood.category_bean.Category_Detail_Info;
import com.qianfeng.missfood.category_bean.Category_Info;
import com.qianfeng.missfood.category_bean.Category_Second_Info;
import com.qianfeng.missfood.fragment.Category_Detail_ptrListView_Fragment;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.List;

public class CategoryDetailsActivity extends AppCompatActivity implements View.OnClickListener{
    @ViewInject(R.id.level2_name)
    private TextView level2_name;
    @ViewInject(R.id.level3_name)
    private TextView level3_name;
    @ViewInject(R.id.down_image)
    private ImageView down_img;
    @ViewInject(R.id.search)
    private ImageView search_img;
    @ViewInject(R.id.details_xiangqing_recyclerview)
    private RecyclerView details_xiangqing_recyclerview;
    @ViewInject(R.id.paixu_click)
    private View paixu_click;
    @ViewInject(R.id.dingwei_click)
    private View dingwei_click;
    @ViewInject(R.id.paixu_text_click)
    private TextView paixu_text_click;
    private TextView moren_text;
    private TextView juli_text;
    private TextView renqi_text;
    private TextView quanguo_text;
    private boolean down_img_flag = true;
    private List<Category_Info> topinfos;
    private List<Category_Second_Info> second_infos;
    private HttpUtils httpUtils = new HttpUtils();
    private String detailUrl;
    private String categoryId;
    private int page = 1;
    private FragmentManager manager;
    private List<String> recyclers = new ArrayList<>();
    private Category_xiangqing_RecyclerView_Adapter recyclerView_adapter;
    private int position;
    private List<String> categoryIds;
    private Category_Detail_Bean bean;
    private PopupWindow paixuPop;
    private boolean flag=true;
    public static int adapterPosition;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category_details);
        ViewUtils.inject(this);
        down_img.setBackgroundResource(R.mipmap.categories_expand_tab_toggle_button_down);
        position = getIntent().getIntExtra("position", 0);
        categoryIds = (List<String>) getIntent().getSerializableExtra("CategoryIds");
        init();
        initData();
        initRecyclerView();
        initRecyclerViewData();
    }


    private void initRecyclerView() {
        LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        details_xiangqing_recyclerview.setLayoutManager(manager);
        recyclerView_adapter = new Category_xiangqing_RecyclerView_Adapter(recyclers, position, getSupportFragmentManager(), categoryIds,level3_name);
        details_xiangqing_recyclerview.setAdapter(recyclerView_adapter);
        //给RecyclerView的每个item之间设置空隙,不会做
    }

    private void initRecyclerViewData() {


        if (getIntent().getSerializableExtra("Infos") != null) {
            if (getIntent().getStringExtra("Tag").equals("Top")) {
                topinfos = (List<Category_Info>) getIntent().getSerializableExtra("Infos");
                for (Category_Info info : topinfos) {
                    recyclers.add(info.getName());
                }
            } else if (getIntent().getStringExtra("Tag").equals("Bottom")) {
                second_infos = (List<Category_Second_Info>) getIntent().getSerializableExtra("Infos");
                for (Category_Second_Info info : second_infos) {
                    recyclers.add(info.getName());
                }
            }
        }
        recyclerView_adapter.notifyDataSetChanged();
    }


    private void init() {
        manager = getSupportFragmentManager();
        categoryId = getIntent().getStringExtra("categoryId");

        detailUrl = "http://api.mishi.cn/index/goods/category/?_method=GET&p-pv=1.0&p-apiv=1.0&categoryId=" + categoryId + "&cityCode=0755&sortType=1&pageSize=10&lng=113.945501&currentPage=" + page + "&lat=22.534877&city=%E6%B7%B1%E5%9C%B3%E5%B8%82%20HTTP/1.1";
        Bundle bundle = new Bundle();
        bundle.putString("categoryId", getIntent().getStringExtra("categoryId"));
        Category_Detail_ptrListView_Fragment detail_ptrListView_fragment = new Category_Detail_ptrListView_Fragment();
        detail_ptrListView_fragment.setArguments(bundle);
        manager.beginTransaction().replace(R.id.details_ptrListView_container, detail_ptrListView_fragment).commit();
        down_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (down_img_flag) {
                    down_img.setBackgroundResource(R.mipmap.categories_expand_tab_toggle_button_up);
                    down_img_flag = false;
                } else if (!down_img_flag) {
                    down_img.setBackgroundResource(R.mipmap.categories_expand_tab_toggle_button_down);
                    down_img_flag = true;
                }


            }
        });
        //点击搜索跳转
        search_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CategoryDetailsActivity.this, Category_Details_Search_Activity.class);
                startActivity(intent);
            }
        });
        //排序点击
        paixu_click.setOnClickListener(this);
        dingwei_click.setOnClickListener(this);
        //popWindow
        View paixuView=getLayoutInflater().inflate(R.layout.category_detail_paixu_popupwindow_layout,null);
        paixuPop = new PopupWindow(paixuView);
        paixuPop.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        paixuPop.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        paixuPop.setBackgroundDrawable(new BitmapDrawable());//必须要设置
        //排序内点击
        moren_text= (TextView) paixuView.findViewById(R.id.moren_text);
        juli_text= (TextView) paixuView.findViewById(R.id.juli_text);
        renqi_text= (TextView) paixuView.findViewById(R.id.renqi_text);
        quanguo_text= (TextView) paixuView.findViewById(R.id.quanguo_text);
        moren_text.setOnClickListener(this);
        juli_text.setOnClickListener(this);
        renqi_text.setOnClickListener(this);
        quanguo_text.setOnClickListener(this);
    }

    private void initData() {
        httpUtils.send(HttpRequest.HttpMethod.GET, detailUrl, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                if (responseInfo != null) {
                    String result = responseInfo.result;
                    bean = JSONObject.parseObject(result, Category_Detail_Bean.class);
                    if (bean != null && bean.getResultList() != null) {
                        //先把头部设置了,因为每条对应的头部都一样
                        if (bean.getResultList().get(0).getGoodsItem().getCategory().getParent() == null) {
                            //上门吃情况
                            level2_name.setText(bean.getResultList().get(0).getGoodsItem().getCategory().getName());
                            level3_name.setText("全部");
                        } else {
                            level2_name.setText(bean.getResultList().get(0).getGoodsItem().getCategory().getParent().getName());
                            level3_name.setText(bean.getResultList().get(0).getGoodsItem().getCategory().getName());
                        }

                    }


                }

            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //排序点击
            case R.id.paixu_click:
                if(flag){
                    paixuPop.showAsDropDown(v);
                    flag =false;
                }else if(!flag){
                    paixuPop.dismiss();
                    flag=true;
                }

                break;
            //排序内点击
            case R.id.moren_text:
                paixu_text_click.setText("默认排序");
                paixuPop.dismiss();
                flag=true;
                recyclerView_adapter.sortType=1;
                recyclerView_adapter.notifyDataSetChanged();
                //更换排序  更换fragment
                Bundle bundle = new Bundle();
                bundle.putString("categoryId", categoryIds.get(adapterPosition));
                bundle.putInt("sortType", recyclerView_adapter.sortType);
                Category_Detail_ptrListView_Fragment detail_ptrListView_fragment = new Category_Detail_ptrListView_Fragment();
                detail_ptrListView_fragment.setArguments(bundle);
                manager.beginTransaction().replace(R.id.details_ptrListView_container, detail_ptrListView_fragment).commit();
                break;
            case R.id.juli_text:
                paixu_text_click.setText("按距离");
                paixuPop.dismiss();
                flag=true;
                recyclerView_adapter.sortType=2;
                recyclerView_adapter.notifyDataSetChanged();

                //更换排序  更换fragment
                Bundle bundle2 = new Bundle();
                bundle2.putString("categoryId", categoryIds.get(adapterPosition));
                bundle2.putInt("sortType", recyclerView_adapter.sortType);
                Category_Detail_ptrListView_Fragment detail_ptrListView_fragment2 = new Category_Detail_ptrListView_Fragment();
                detail_ptrListView_fragment2.setArguments(bundle2);
                manager.beginTransaction().replace(R.id.details_ptrListView_container, detail_ptrListView_fragment2).commit();
                break;
            case R.id.renqi_text:
                paixu_text_click.setText("按人气");
                paixuPop.dismiss();
                flag=true;
                recyclerView_adapter.sortType=3;
                recyclerView_adapter.notifyDataSetChanged();

                //更换排序  更换fragment
                Bundle bundle3 = new Bundle();
                bundle3.putString("categoryId", categoryIds.get(adapterPosition));
                bundle3.putInt("sortType", recyclerView_adapter.sortType);
                Category_Detail_ptrListView_Fragment detail_ptrListView_fragment3 = new Category_Detail_ptrListView_Fragment();
                detail_ptrListView_fragment3.setArguments(bundle3);
                manager.beginTransaction().replace(R.id.details_ptrListView_container, detail_ptrListView_fragment3).commit();
                break;
            case R.id.quanguo_text:
                paixu_text_click.setText("全国推荐");
                paixuPop.dismiss();
                flag=true;
                recyclerView_adapter.sortType=4;
                recyclerView_adapter.notifyDataSetChanged();

                //更换排序  更换fragment
                Bundle bundle4 = new Bundle();
                bundle4.putString("categoryId", categoryIds.get(adapterPosition));
                bundle4.putInt("sortType", recyclerView_adapter.sortType);
                Category_Detail_ptrListView_Fragment detail_ptrListView_fragment4 = new Category_Detail_ptrListView_Fragment();
                detail_ptrListView_fragment4.setArguments(bundle4);
                manager.beginTransaction().replace(R.id.details_ptrListView_container, detail_ptrListView_fragment4).commit();
                break;
            //定位点击
            case R.id.dingwei_click:

                break;

        }


    }
}
